package com.example.composableexample.snackbar

import androidx.compose.material.SnackbarDefaults
import androidx.compose.material.SnackbarDuration
import androidx.compose.material.SnackbarHostState
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import dagger.Component
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

enum class SnackBarState {
    DEFAULT,
    ERROR
}

class SnackBarDelegate(
    var snackBarHostState: SnackbarHostState? = null,
    var coroutineScope: CoroutineScope? = null
) {
    private var snackBarState: SnackBarState = SnackBarState.DEFAULT

    val snackBarBackgroundColor: Color
        @Composable
        get() = when (snackBarState) {
            SnackBarState.DEFAULT -> SnackbarDefaults.backgroundColor
            SnackBarState.ERROR -> Color.Red
        }

    fun showSnackBar(
        state: SnackBarState,
        message: String,
        actionLabel: String? = null,
        duration: SnackbarDuration = SnackbarDuration.Short
    ) {
        this.snackBarState = state
        coroutineScope?.launch {
            snackBarHostState?.showSnackbar(
                message = message,
                actionLabel = actionLabel,
                duration = duration
            )
        }
    }
}